﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Net.Mail;
using OuterParser.Models;

namespace OuterParser.OsvitaUa
{
    public class OsvitaParserInterface
    {
        /// <summary>
        /// Starts the parsing process.
        /// </summary>
        public static void Launch()
        {
            try
            {
                OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog("Start parsing...");
                Console.WriteLine("Start parsing...");
                string href = @"http://osvita.ua/vnz/guide/";
                OuterParser.OsvitaUa.ParserOsivtaUa parO = new OuterParser.OsvitaUa.ParserOsivtaUa();
                //  parO.GetAllInfoAboutUniv(@"http://osvita.ua/vnz/guide/17/398/");
                parO.GetDirectionInPage(href, true);
                Console.WriteLine("Directions add ");
                OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog("Directions add");
                foreach (string d in parO.dirsHref)
                {
                    parO.GetUnivsPageByDirection(1, parO.mainHref + d);
                }
                OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog("All univs add");
                Console.WriteLine("All univs add");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog(string.Format("error in test project \n{0}", e.ToString()));
            }
        }
    }
    class ParserOsivtaUa
    {
        public string mainHref = @"http://osvita.ua";
        public string imageFolderPath = @"sourse";
        public int countUniv = 0;
        public List<string> dirsHref = new List<string>();
        public void GetDirectionInPage(string href, bool writeDirecteionInDb)
        {
            int i = 0;
            do
            {
                try
                {
                    WebRequest firstRequest = WebRequest.Create(href);

                    using (var response = firstRequest.GetResponse())
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(stream, Encoding.GetEncoding(65001)))//utf-8
                            {
                                string allText = reader.ReadToEnd();
                                allText = allText.Remove(0, allText.IndexOf(">Пошук вузів за напрямами підготовки</a>"));
                                allText = allText.Remove(0, allText.IndexOf("</div>") + 6);
                                allText = allText.Remove(allText.IndexOf("<div class=\"block w620 hauto\">"));

                                allText = allText.Remove(0, allText.IndexOf("<li>"));
                                allText = allText.Remove(allText.LastIndexOf("</ul>"));

                                string allLi = allText.Remove(allText.IndexOf("</ul>"));
                                allText = allText.Remove(0, allText.IndexOf("listlinks"));
                                allLi += allText.Remove(0, allText.IndexOf(">") + 1);
                                int index = 0;
                                string dir = "";
                                string str = "";
                                do
                                {
                                    index = allLi.IndexOf("</a>");
                                    if (index == -1) break;

                                    str = allLi.Remove(index);
                                    allLi = allLi.Remove(0, index + 4);

                                    dir = str.Remove(0, str.LastIndexOf(">") + 1);
                                    str = str.Remove(str.LastIndexOf("\">"));
                                    str = str.Remove(0, str.LastIndexOf("\"") + 1);

                                    dirsHref.Add(str);
                                    if (writeDirecteionInDb)
                                    {
                                        OsvitaUaServices.AddDirectionToDb(dir);
                                    }

                                }
                                while (true);
                                return;

                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Notification.AddErrorToErrorsLog(string.Format("error parse direction href={0} \n{1}", href, e.ToString()));
                    if (i > 10) i++;
                    else break;
                }
            } while (true);

        }
        public void GetUnivsPageByDirection(int countOfPage, string href)
        {

            int i = 0;
            do
            {
                try
                {
                    WebRequest firstRequest = WebRequest.Create(href);

                    using (var response = firstRequest.GetResponse())
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(stream, Encoding.GetEncoding(65001)))//utf-8
                            {
                                string allText = reader.ReadToEnd();
                                string table = allText.Remove(0, allText.IndexOf("guide list top"));
                                table = table.Remove(0, table.IndexOf("<tbody>") + 7);
                                table = table.Remove(table.IndexOf("</tbody>"));
                                GetUnivsByTable(table);
                                if (allText.Contains("pages clear"))
                                {
                                    string pages = allText.Remove(0, allText.IndexOf("pages clear"));
                                    pages = pages.Remove(pages.IndexOf("</ul>"));
                                    if (pages.Contains(string.Format(">{0}<", countOfPage + 1)))
                                    {
                                        pages = pages.Remove(pages.IndexOf(string.Format("\">{0}<", countOfPage + 1)));
                                        pages = pages.Remove(0, pages.LastIndexOf("\"") + 1);
                                        GetUnivsPageByDirection(countOfPage + 1, mainHref + pages);
                                    }
                                }
                                return;

                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Notification.AddErrorToErrorsLog(string.Format("error with page of univ href ={0} \n {1}", href, e.ToString()));
                    if (i > 10) i++;
                    else break;
                }
            } while (true);

        }
        public void GetUnivsByTable(string table)
        {
            try
            {
                int index;
                string row = "";
                string address = "";
                string name = "";
                do
                {
                    index = table.IndexOf("</tr>");
                    if (index == -1) break;
                    row = table.Remove(index - 5);
                    table = table.Remove(0, index + 5);

                    address = row.Remove(0, row.IndexOf("href=\""));
                    address = address.Remove(0, address.IndexOf("\"") + 1);
                    address = address.Remove(address.IndexOf("\""));
                    name = row.Remove(0, row.IndexOf("title=\"") + 7);
                    name = name.Remove(name.IndexOf("\">"));

                    GetAllInfoAboutUniv(mainHref + address);

                }
                while (true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Notification.AddErrorToErrorsLog(string.Format("error get univ by table \n{0}", e.ToString()));
            }
        }

        public string GetTextInRowForTopTable(string row)
        {
            string info = row.Remove(0, row.IndexOf("<td>") + 4);
            info = info.Remove(0, info.IndexOf("<td>") + 4);
            info = info.Remove(info.IndexOf("</td>"));
            if (info.Contains("<a"))
            {
                info = info.Remove(info.LastIndexOf("<"));
                info = info.Remove(0, info.LastIndexOf(">") + 1);
            }
            return info;
        }
        public string GetSityInRowForTopTable(string row)
        {
            string info = row.Remove(0, row.IndexOf("<td>") + 4);
            info = info.Remove(0, info.IndexOf("<td>") + 4);
            info = info.Remove(info.IndexOf("</td>"));
            if (info.Contains("</a>"))
            {
                info = info.Remove(info.LastIndexOf("</a>"));
                info = info.Remove(0, info.LastIndexOf(">") + 1);
            }
            return info;
        }
        public string GetTextFullInRowTopTable(string row)
        {
            string key = row.Remove(row.IndexOf("</a>"));
            key = key.Remove(0, key.LastIndexOf(">") + 1);
            string val = row.Remove(row.LastIndexOf("</td>"));
            val = val.Remove(0, val.LastIndexOf("<td>") + 4);
            return string.Format("[{0}:{1}]", key, val);
        }
        public void GetAllInfoAboutUniv(string href)
        {
            int i = 0;
            do
            {
                try
                {
                    WebRequest firstRequest = WebRequest.Create(href);

                    using (var response = firstRequest.GetResponse())
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(stream, Encoding.GetEncoding(65001)))//utf-8
                            {
                                int index = 0;
                                string allText = reader.ReadToEnd();
                                allText = allText.Remove(0, allText.IndexOf("\"article\""));
                                allText = allText.Remove(allText.IndexOf("\"artbanner\""));
                                allText = allText.Remove(allText.LastIndexOf("</div>"));
                                allText = allText.Remove(0, allText.IndexOf("\">") + 2);
                                //allText == div class="article"

                                //parse top block
                                #region TopBlock

                                string topBlock = allText.Remove(0, allText.IndexOf("\"topimg\""));
                                topBlock = topBlock.Remove(topBlock.IndexOf("</table>"));

                                //image
                                string imgSource = topBlock.Remove(0, topBlock.IndexOf("src=\"") + 5);
                                imgSource = imgSource.Remove(imgSource.IndexOf("\""));
                                if (imgSource.Contains("No_logo_m")) imgSource = "";

                                topBlock = topBlock.Remove(0, topBlock.IndexOf("</td>") + 5);
                                topBlock = topBlock.Remove(0, topBlock.IndexOf("<td"));
                                topBlock = topBlock.Remove(0, topBlock.IndexOf(">"));
                                topBlock = topBlock.Remove(topBlock.IndexOf("</td>"));
                                string tel = "";
                                string mail = "";
                                string site = "";
                                string address = "";
                                string str = "";
                                string location = "";
                                do
                                {
                                    index = topBlock.IndexOf("<br />");
                                    if (index == -1) break;
                                    str = topBlock.Remove(index);
                                    topBlock = topBlock.Remove(0, index + 4);
                                    //location
                                    if (str.Contains("(карта)"))
                                    {
                                        address = str.Remove(0, str.IndexOf(">") + 1);
                                        address = address.Remove(address.IndexOf("&nbsp"));
                                        address = address.Replace("\n", "").Replace("\t", "");
                                        continue;
                                    }
                                    //mail
                                    if (str.Contains("unescape"))
                                    {
                                        str = str.Remove(str.LastIndexOf("\'"));
                                        str = str.Remove(0, str.IndexOf("\'") + 1);
                                        str = Uri.UnescapeDataString(str);
                                        str = str.Remove(0, str.IndexOf("\'"));
                                        str = str.Remove(str.LastIndexOf("\'"));
                                        mail = str.Remove(0, str.IndexOf('>') + 1);
                                        mail = mail.Remove(mail.IndexOf("<"));

                                        continue;
                                    }
                                    //site
                                    if (str.Contains("http://"))
                                    {
                                        site = str.Remove(str.IndexOf("</a>"));
                                        site = site.Remove(0, site.LastIndexOf("\">") + 2);
                                        continue;
                                    }
                                    //tel
                                    if (str.Contains("(") && str.Contains(")"))
                                    {
                                        if (str.Contains(">"))
                                        {
                                            str = str.Remove(0, str.IndexOf(">") + 1);
                                        }
                                        tel += str.Replace("\"", "").Replace("\n", "").Replace("\t", "").Replace(">", "") + ",";
                                    }
                                }
                                while (true);
                               if(tel.Length>5)   tel = tel.Remove(tel.LastIndexOf(","));
                                #endregion


                                //name
                                string name = allText.Remove(0, allText.IndexOf("heading"));
                                name = name.Remove(name.IndexOf("</h1>"));
                                name = name.Remove(0, name.LastIndexOf(">") + 1);
                                string nameShort ="";
                                string[] nStr = name.Split(' ');
                                if (name.Contains("("))
                                {
                                    nameShort = name.Remove(name.IndexOf(")"));
                                    nameShort = nameShort.Remove(0, nameShort.IndexOf("(") + 1);
                                    name = name.Remove(name.IndexOf("("));
                                }
                                else
                                {
                                    if (nStr[nStr.Length - 1].Length < 10)
                                    {
                                        nameShort = nStr[nStr.Length - 1];
                                        name = name.Replace(nameShort,"");
                                    }
                                }

                                //if (href == @"http://osvita.ua/vnz/guide/17/16263/")
                                //{
                                //    Console.ReadLine();
                                //}

                                //add info
                                string addInfo = "";

                                str = allText.Remove(0, allText.IndexOf("desc_ext") + 8);
                                str = str.Remove(0, str.IndexOf("desc_ext") + 8);
                                str = str.Remove(str.IndexOf("propos"));
                                if (str.Length > 50)
                                {
                                    str = str.Remove(0, str.IndexOf("</p>") + 4);
                                    str = str.Remove(0, str.IndexOf("</p>") + 4);
                                    addInfo = str.Remove(str.LastIndexOf("</div>"));
                                }

                                //parse tables
                                #region Parse Tables
                                //top table
                                #region TopTable
                                str = allText.Remove(0, allText.IndexOf("description"));
                                str = str.Remove(0, str.IndexOf("<table"));
                                str = str.Remove(str.IndexOf("</table>"));
                                string line = "";
                                string type = "";
                                string ownership = "";
                                string acreditation = "";
                                string costOfStudy = "";
                                string formOfStudy = "";
                                string qualificationLevel = "";
                                string sity = "";
                                string rating = "";
                                do
                                {
                                    index = str.IndexOf("</tr>");
                                    if (index == -1) break;
                                    line = str.Remove(index);
                                    str = str.Remove(0, index + 5);

                                    //type
                                    if (line.Contains("Тип навчального закладу"))
                                    {
                                        type = GetTextInRowForTopTable(line);
                                        continue;
                                    }
                                    //ownship
                                    if (line.Contains("Форма власності"))
                                    {
                                        ownership = GetTextInRowForTopTable(line);
                                        continue;
                                    }
                                    //acreditation level
                                    if (line.Contains("Рівень акредитації"))
                                    {
                                        acreditation = GetTextInRowForTopTable(line);
                                        continue;
                                    }
                                    //cost of study
                                    if (line.Contains("Вартість навчання на рік"))
                                    {
                                        costOfStudy = GetTextInRowForTopTable(line);
                                        continue;
                                    }
                                    //form of study
                                    if (line.Contains("Форма навчання"))
                                    {
                                        formOfStudy = GetTextInRowForTopTable(line);
                                        continue;
                                    }
                                    //qualification level
                                    if (line.Contains("Кваліфікаційний рівень"))
                                    {
                                        qualificationLevel = GetTextInRowForTopTable(line);
                                        continue;
                                    }
                                    //sity
                                    if (line.Contains("Місто"))
                                    {
                                        sity = GetSityInRowForTopTable(line);
                                        continue;
                                    }
                                    //rating
                                    if (line.Contains("Рейтинг університетів"))
                                    {
                                        rating += GetTextFullInRowTopTable(line) + ",";
                                    }

                                } while (true);
                                if (rating.Contains(","))
                                {
                                    rating = rating.Remove(rating.LastIndexOf(","));
                                }
                                #endregion

                                //left table
                                #region Left Table
                                bool freeTraining = false;
                                bool paidTraining = false;
                                bool postgraduateEducation = false;
                                bool postgraduateDoctoral = false;
                                bool militaryDepartment = false;
                                bool hostel = false;
                                bool trainingDepartment = false;
                                bool preparationCoursesLessons = false;
                                if (allText.Contains("gutab2"))
                                {
                                    str = allText.Remove(0, allText.IndexOf("gutab2"));
                                    str = str.Remove(str.IndexOf("</table>"));
                                    str = str.Remove(0, str.IndexOf(">") + 1);

                                    do
                                    {
                                        index = str.IndexOf("</tr>");
                                        if (index == -1) break;
                                        line = str.Remove(index);
                                        str = str.Remove(0, index + 5);
                                        if (line.Contains("Безкоштовне навчання"))
                                        {
                                            freeTraining = line.Contains("&#149;");
                                            continue;
                                        }
                                        if (line.Contains("Платне навчання"))
                                        {
                                            paidTraining = line.Contains("&#149;");
                                            continue;
                                        }
                                        if (line.Contains("Післядипломна освіта"))
                                        {
                                            postgraduateEducation = line.Contains("&#149;");
                                            continue;
                                        }
                                        if (line.Contains("Аспірантура, докторантура"))
                                        {
                                            postgraduateDoctoral = line.Contains("&#149;");
                                            continue;
                                        }
                                        if (line.Contains("Військова кафедра"))
                                        {
                                            militaryDepartment = line.Contains("&#149;");
                                            continue;
                                        }
                                        if (line.Contains("Гуртожиток"))
                                        {
                                            hostel = line.Contains("&#149;");
                                            continue;
                                        }
                                        if (line.Contains("Підготовче відділення"))
                                        {
                                            trainingDepartment = line.Contains("&#149;");
                                            continue;
                                        }
                                        if (line.Contains("Курси підготовки до ЗНО"))
                                        {
                                            preparationCoursesLessons = line.Contains("&#149;");
                                            continue;
                                        }
                                    }
                                    while (true);
                                }
                                #endregion

                                //direction
                                #region Direction
                                str = allText.Remove(0, allText.IndexOf("за такими напрямами"));
                                str = str.Remove(0, str.IndexOf("<ul"));
                                str = str.Remove(0, str.IndexOf(">") + 1);
                                str = str.Remove(str.IndexOf("</ul>"));
                                List<string> dirs = new List<string>();
                                do
                                {
                                    index = str.IndexOf("</li>");
                                    if (index == -1) break;
                                    line = str.Remove(index);
                                    str = str.Remove(0, index + 5);
                                    line = line.Remove(line.IndexOf("</a>"));
                                    line = line.Remove(0, line.LastIndexOf(">") + 1);
                                    dirs.Add(line);
                                }
                                while (true);
                                #endregion

                                //speciality
                                #region speciality
                                List<KeyValuePair<string, List<string>>> dirSpec = new List<KeyValuePair<string, List<string>>>();
                                List<string> spec;
                                string allT = allText.Remove(0, allText.IndexOf("gtag"));
                                allT = allT.Remove(0, allT.IndexOf("</ul>") + 5);
                                string s = "";
                                foreach (string d in dirs)
                                {
                                    // str = allText.Remove(0, allText.IndexOf(string.Format("СПЕЦІАЛЬНОСТІ НАПРЯМУ \"{0}", d.ToUpper())));
                                    s = d.ToUpper();
                                    bool flag = false;
                                    if (!allT.Contains(s))
                                    {
                                        string[] g = s.Split(' ');
                                        for (int j = 0; j < g.Length; j++)
                                        {
                                            if (g[j].Length > 3 && allT.Contains(g[j]))
                                            {
                                                s = g[j];
                                                flag = true;
                                            }
                                        }
                                        if (!flag) continue;
                                    }
                                    str = allT.Remove(0, allT.IndexOf(s));
                                    str = str.Remove(0, str.IndexOf("<ul"));
                                    str = str.Remove(0, str.IndexOf(">") + 1);
                                    str = str.Remove(str.IndexOf("</ul>"));
                                    spec = new List<string>();
                                    do
                                    {
                                        index = str.IndexOf("</li>");
                                        if (index == -1) break;
                                        line = str.Remove(index);
                                        str = str.Remove(0, index + 5);
                                        line = line.Remove(0, line.IndexOf(">") + 1);
                                        if (line.Contains("<strong>"))
                                        {
                                            line = line.Remove(line.IndexOf("<strong"));
                                            line = line.Remove(0, line.IndexOf(">") + 1);
                                        }
                                        spec.Add(line.Replace(".", ""));
                                    }
                                    while (true);
                                    dirSpec.Add((new KeyValuePair<string, List<string>>(d, spec)));
                                }
                                #endregion

                                #endregion

                                OsvitaUa_University univ = new OsvitaUa_University
                                {
                                    Name = name,
                                    ShortName = nameShort,
                                    Tel = tel,
                                    Mail = mail,
                                    Website = site,
                                    AddressUniv = address,
                                    CoordinatesMap = location,
                                    LogoPath = imgSource,
                                    UniversityType = type,
                                    Ownershipp = ownership,
                                    CostOfTraining = costOfStudy,
                                    FormOfStady = formOfStudy,
                                    Sity = sity,
                                    Ratings = rating,
                                    DetailInfo = addInfo,
                                    SourceSite = href,
                                    FreeStady = freeTraining,
                                    PaidStady = paidTraining,
                                    PostgraduateEducation = postgraduateEducation,
                                    PostgraduateDoctoral = postgraduateDoctoral,
                                    MilitaryDepartment = militaryDepartment,
                                    Hostel = hostel,
                                    TraininingDepartment = trainingDepartment,
                                    PreparationCourses = preparationCoursesLessons,
                                    AccreditationLevelName = acreditation
                                };

                                OsvitaUaServices.AddAllInfoAboutUniv(univ, qualificationLevel, dirSpec,href);
                                countUniv++;
                                Console.WriteLine(countUniv);
                                return;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Notification.AddErrorToErrorsLog(e.ToString());
                    if (i > 10) i++;
                    else break;
                }
            } while (true);
        }

    }
    class OsvitaUaServices
    {
        static PulsatrixStagingDbEntities db = new PulsatrixStagingDbEntities();
        static string path = @"C:\pulsatrix\source\";
        static int id = 0;

        #region NextId
        //public static int GetNextIdInDirection()
        //{
        //    if (db.Direction.Count() > 0)
        //    {
        //        return db.Direction.Max(x => x.DirectionId) + 1;

        //    }
        //    else return 1;
        //}
        //public static int GetNextIdInUniversity()
        //{
        //    if (db.University.Count() > 0)
        //    {
        //        return db.University.Max(x => x.UniversityId) + 1;
        //    }
        //    return 1;
        //}
        //public static int GetNextIdInUnivSpec()
        //{
        //    if (db.UniversitySpeciality.Count() > 0)
        //    {
        //        return db.UniversitySpeciality.Max(x => x.id) + 1;
        //    }
        //    return 1;
        //}
        //public static int GetNextIdQualificationLevel()
        //{
        //    if (db.QualificationLevel.Count() > 0)
        //    {
        //        return db.QualificationLevel.Max(x => x.QualificationLevelId) + 1;
        //    }
        //    return 1;
        //}
        //public static int GetNextSpecialityId()
        //{
        //    if (db.Speciality.Count() > 0)
        //    {
        //        return db.Speciality.Max(x => x.SpecialityId) + 1;
        //    }
        //    return 1;
        //}
        //public static int GetNextQualificationLevelOfUnivId()
        //{
        //    if (db.QualificationOfUniversity.Count() > 0)
        //    {
        //        return db.QualificationOfUniversity.Max(x => x.QualificationOfUniversityId);
        //    }
        //    return 1;
        //}
        #endregion
        public static int SearchUnivIdByName(string name)
        {

            var a = db.OsvitaUa_University.FirstOrDefault(x => x.Name == name);
            if (a == null) return -1;
            return a.Id;

        }
        public static string SearchUnivLogoPathByName(int id)
        {
            OsvitaUa_University u = db.OsvitaUa_University.FirstOrDefault(x => x.Id == id);
            if (u == null) return null;
            return string.Format("{0}{1}", path, u.LogoPath);
        }
        public static bool SearchDirectionInDbByNameB(string dirName)
        {
            if (db.OsvitaUa_Direction.FirstOrDefault(x => x.Name == dirName) == null) return false;
            return true;
        }
        public static int SearchDirectionInDbByName(string dirName)
        {
            OsvitaUa_Direction a = db.OsvitaUa_Direction.FirstOrDefault(x => x.Name == dirName);
            if (a == null) return -1;
            return a.Id;
        }
        public static int SearchQualificationLevelId(string qLName)
        {
            OsvitaUa_QualificationLevel ql = db.OsvitaUa_QualificationLevel.FirstOrDefault(x => x.Name == qLName);
            if (ql == null) return -1;
            return ql.Id;
        }
        public static int SearchSpecialityId(string name)
        {
            var a = db.OsvitaUa_Speciality.FirstOrDefault(x => x.Name == name);
            if (a == null) return -1;
            return a.Id;
        }

        public static void AddDirectionToDb(string dirName)
        {
            try
            {
                int index = SearchDirectionInDbByName(dirName);
                if (index != -1) return;
                db.OsvitaUa_Direction.Add(new OsvitaUa_Direction() { Name = dirName });
                db.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Notification.AddErrorToErrorsLog(string.Format("filed to add direction {0} \n {1}", dirName, e.ToString()));
                
            }
        }
        public static int AddSpeciality(string spec, string dir)
        {
            try
            {
                int index = SearchSpecialityId(spec);
                if (index != -1) return index;
                int dirId = SearchDirectionInDbByName(dir);
                if (dirId == -1)
                {
                    AddDirectionToDb(dir);
                    dirId = SearchDirectionInDbByName(dir);
                }

                db.OsvitaUa_Speciality.Add(new OsvitaUa_Speciality() { Name = spec, DirectionId = dirId });
                db.SaveChanges();

                return SearchSpecialityId(spec);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Notification.AddErrorToErrorsLog(string.Format("filed to add speciality {0}",e.ToString()));
                return -1;
            }
        }
        public static void AddUnivSpeciality(int specId, int univId)
        {
            try
            {
                db.OsvitaUa_UniversitySpeciality.Add(new OsvitaUa_UniversitySpeciality() { SpecityId = specId, UnivesityId = univId });
                db.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Notification.AddErrorToErrorsLog(string.Format("filed to add UnivSpeciality {0} ",e.ToString()));
            }
        }
        public static int AddQualificationLevel(string name)
        {
            int index = SearchQualificationLevelId(name);
            if (index != -1) return index;
            db.OsvitaUa_QualificationLevel.Add(new OsvitaUa_QualificationLevel() { Name = name });
            db.SaveChanges();
            return SearchQualificationLevelId(name);
        }
        public static void AddQualificationLevelOfUniv(int qualificationLevelId, int univId)
        {
            db.OsvitaUa_QualificationOfUniversity.Add(new OsvitaUa_QualificationOfUniversity()
            {
                QualificationLevelId = qualificationLevelId,
                UniversityId = univId
            });
            db.SaveChanges();
        }
        public static bool AddAllInfoAboutUniv(OsvitaUa_University univ, string qualificationLevel, List<KeyValuePair<string, List<string>>> dirSpec, string href)
        {
            try
            {
                if (href == @"http://osvita.ua/vnz/guide/17/78/")
                {
                    Console.WriteLine();
                }
                id = SearchUnivIdByName(univ.Name);
                if (id != -1) return false;
                int index = 0;
                string dt = univ.DetailInfo;
                univ.DetailInfo = "";

                //add univ

                db.OsvitaUa_University.Add(univ);
                db.SaveChanges();
                id = SearchUnivIdByName(univ.Name);

                //download image
                if (univ.LogoPath != "")
                {
                    WebClient wc = new WebClient();
                    wc.DownloadFile(univ.LogoPath, path + id + ".jpg");
                    univ.LogoPath = id + ".jpg";
                }
                else
                {
                    univ.LogoPath = "noLogo.jpg";
                }

                //add addInfo
                if (dt != "")
                {
                    FileStream fs = new FileStream(path + id + ".txt", FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(dt);
                    sw.Close();
                    fs.Close();
                    univ.DetailInfo = id + ".txt";
                }

                //update univ record
                db.OsvitaUa_University.FirstOrDefault(x => x.Id == id).LogoPath = univ.LogoPath;
                if (dt != "") db.OsvitaUa_University.FirstOrDefault(x => x.Id == id).DetailInfo = univ.DetailInfo;
                db.SaveChanges();

                //add speciality and univSpeciality
                foreach (var ds in dirSpec)
                {
                    foreach (var s in ds.Value)
                    {
                        index = AddSpeciality(s, ds.Key);
                        AddUnivSpeciality(index, id);
                    }
                }

                //add qualification level
                string[] ql = qualificationLevel.Split(',');
                foreach (string s in ql)
                {
                    index = AddQualificationLevel(s);
                    AddQualificationLevelOfUniv(index, id);
                }

                db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Notification.AddErrorToErrorsLog(string.Format("error. metod addAllInfoAboutUniversity \n{0}", e));
                RemoveErrorRecord(id);
                return false;
            }
        }

        public static bool RemoveUniv(int univId)
        {
            try
            {
                OsvitaUa_University u = db.OsvitaUa_University.FirstOrDefault(x => x.Id == univId);
                if (u == null) return false;
                db.OsvitaUa_University.Remove(u);
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
        public static bool RemoveUnivSpec(int univId)
        {
            try
            {
                OsvitaUa_UniversitySpeciality us = db.OsvitaUa_UniversitySpeciality.FirstOrDefault(x => x.UnivesityId == univId);
                if (us == null) return false;
                db.OsvitaUa_UniversitySpeciality.Remove(us);
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }
        public static bool RemoveQualificationOfUniversity(int univId)
        {
            try
            {
                OsvitaUa_QualificationOfUniversity qu = db.OsvitaUa_QualificationOfUniversity.FirstOrDefault(x => x.Id == univId);
                if (qu == null) return false;
                db.OsvitaUa_QualificationOfUniversity.Remove(qu);
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
        }
        public static bool RemoveImage(string pathLogo)
        {
            try
            {
                if (File.Exists(pathLogo))
                {
                    File.Delete(pathLogo);
                    return true;
                }
                else
                {
                    Notification.AddErrorToErrorsLog(string.Format("file not exist path={0}", pathLogo));
                    return false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Notification.AddErrorToErrorsLog(string.Format("failed remove image path={0} \n "));
                return false;
            }
        }
        public static void RemoveErrorRecord(int i)
        {
            string lofoPath = "";
            if (!RemoveQualificationOfUniversity(i)) Notification.AddErrorToErrorsLog(string.Format("failed to remove recodr idUniv={0} int table QualificationOfUniversity", i));
            if (!RemoveUnivSpec(i)) Notification.AddErrorToErrorsLog(string.Format("failed to remove recodr idUniv={0} int table UniversitySpeciality", i));
            if (!RemoveUniv(i)) Notification.AddErrorToErrorsLog(string.Format("failed to remove recodr id={0} int table University", i));
            lofoPath = SearchUnivLogoPathByName(i);
            if (lofoPath != null) RemoveImage(lofoPath);
        }
    }
    class Notification
    {

        public static void AddErrorToErrorsLog(string info)
        {
            if (!Directory.Exists(@"C:\pulsatrix"))
                Directory.CreateDirectory(@"C:\pulsatrix");
            FileStream fs = new FileStream(@"C:\pulsatrix\ErrorsLog.txt", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(DateTime.Now);
            sw.WriteLine(info);
            sw.WriteLine();
            sw.WriteLine();
            sw.Close();
            fs.Close();
        }
        public static void SendEmail(StringBuilder text, string mailbox)
        {
            SmtpClient Smtp = new SmtpClient("smtp.gmail.com", 587);
            Smtp.UseDefaultCredentials = false;
            Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            Smtp.EnableSsl = true;
            Smtp.Credentials = new NetworkCredential("_______@gmail.com", "PASSWORD");
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("___________@gmail.com");
            mail.Subject = "Registration...";
            mail.Body = text.ToString();
            //Attachment attach = new Attachment("filename"); ;
            //mail.Attachments.Add(attach);
            mail.To.Add(mailbox);
            Smtp.Send(mail);
        }
    }
}
